<?php

/**
 * evenement actions.
 *
 * @package    sinc
 * @subpackage evenement
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class evenementActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->evenement_list = EvenementPeer::doSelect(new Criteria());
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->date = $request->getParameter('date');

    $this->id_user = $this->getUser()->getGuardUser()->getId();

    $c = new Criteria();
   $c->add(PagePeer::ID_USER,$this->id_user);
   $this->css = PagePeer::doSelect($c);

    $evenement = new Evenement();
    $evenement->setDateCreation($this->date);
    $evenement->setDate($this->date);

    $this->form = new EvenementForm($evenement);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));


    $this->form = new EvenementForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($evenement = EvenementPeer::retrieveByPk($request->getParameter('id_evenement')), sprintf('Object evenement does not exist (%s).', $request->getParameter('id_evenement')));
    $this->form = new EvenementForm($evenement);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($evenement = EvenementPeer::retrieveByPk($request->getParameter('id_evenement')), sprintf('Object evenement does not exist (%s).', $request->getParameter('id_evenement')));
    $this->form = new EvenementForm($evenement);

    $this->processForm2($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($evenement = EvenementPeer::retrieveByPk($request->getParameter('id_evenement')), sprintf('Object evenement does not exist (%s).', $request->getParameter('id_evenement')));
    $evenement->delete();

    $this->redirect('agenda/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $evenement = $form->save();
      $this->redirect('agenda/enregistrement?id_evenement='.$evenement->getIdEvenement().'');
    }
  }

   protected function processForm2(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $evenement = $form->save();
      $this->redirect('agenda/index');
    }
  }
}
